/*****************************************
  NAME: IIC.c
  DESC: IIC test(Interrupt / Non Interrupt (Polling)
  WWW.YCTEK.COM
 *****************************************/
#include <string.h>
#include "2410addr.h"
#include "2410lib.h"
#include "def.h"
#include "IIC.h"

static U8 _iicData[IICBUFSIZE];
static volatile int _iicDataCount;
static volatile int _iicStatus;
static volatile int _iicMode;
static int _iicPt;


void * func_iic_test[][2]=
{	
	(void *)Test_Iic,"IIC(24C02)INT",
	(void *)Test_Iic2,"IIC(24C02)POL",
	0,0
};

void Iic_Test(void)
{
	int i;
	
	Uart_Printf("\n======  IIC Test program start ======\n");
		
	while(1)
	{
		i=0;
		Uart_Printf("\n\n");
		while(1)
		{
			Uart_Printf("%2d:%s",i,func_iic_test[i][1]);
			i++;
			if((int)(func_iic_test[i][0])==0)
			{
				Uart_Printf("\n");
				break;
			}
			if((i%4)==0)
			Uart_Printf("\n");
		}

		Uart_Printf("\nPress Enter key to exit : ");
		i = Uart_GetIntNum();
		if(i==-1) break;
		if(i>=0 && (i<((sizeof(func_iic_test)-1)/8)) )
			( (void (*)(void)) (func_iic_test[i][0]) )();
	}
	
	Uart_Printf("\n====== IIC Test program end ======\n");
}

void Test_Iic(void)
{
    unsigned int i,j,save_E,save_PE;
    static U8 data[256];

    Uart_Printf("[ IIC Test(Interrupt) using 24C02 ]\n");

    save_E   = rGPECON;
    save_PE  = rGPEUP;
    rGPEUP  = 0xc000;
    rGPECON = 0xa0000000;

    pISR_IIC = (unsigned)IicInt;
    rINTMSK &= ~(BIT_IIC);

    rIICCON = (1<<7) | (0<<6) | (1<<5) | (0xf);

    rIICADD  = 0x10; 
    rIICSTAT = 0x10;
	rIICLC = (1<<2)|(1);
    
    Uart_Printf("Write test data into 24C02\n");

    for(i=0;i<256;i++)
        Wr24C080(0xa0,(U8)i,i);
           
    for(i=0;i<256;i++)
        data[i] = 0;

    Uart_Printf("Read test data from 24C02\n");
    
    for(i=0;i<256;i++)
        Rd24C080(0xa0,(U8)i,&(data[i])); 

    for(i=0;i<16;i++)
    {
        for(j=0;j<16;j++)
            Uart_Printf("%2x ",data[i*16+j]);
        Uart_Printf("\n");
    }
    rINTMSK |= BIT_IIC;    
    rGPEUP  = save_PE;
    rGPECON = save_E;
}

void Wr24C080(U32 slvAddr,U32 addr,U8 data)
{
    _iicMode      = WRDATA;
    _iicPt        = 0;
    _iicData[0]   = (U8)addr;
    _iicData[1]   = data;
    _iicDataCount = 2;
    
    rIICDS   = slvAddr;
    rIICSTAT = 0xf0;
    
    while(_iicDataCount!=-1);

    _iicMode = POLLACK;

    while(1)
    {
        rIICDS     = slvAddr;
        _iicStatus = 0x100;
        rIICSTAT   = 0xf0;
        rIICCON    = 0xaf;
           
        while(_iicStatus==0x100);
           
        if(!(_iicStatus&0x1))
            break;
    }
    rIICSTAT = 0xd0;
    rIICCON  = 0xaf;
    Delay(1);
}
        
void Rd24C080(U32 slvAddr,U32 addr,U8 *data)
{
    _iicMode      = SETRDADDR;
    _iicPt        = 0;
    _iicData[0]   = (U8)addr;
    _iicDataCount = 1;

    rIICDS   = slvAddr;
    rIICSTAT = 0xf0;

    while(_iicDataCount!=-1);

    _iicMode      = RDDATA;
    _iicPt        = 0;
    _iicDataCount = 1;
    
    rIICDS        = slvAddr;
    rIICSTAT      = 0xb0;
    rIICCON       = 0xaf;  
    while(_iicDataCount!=-1);

    *data = _iicData[1];
}

void __irq IicInt(void)
{
    U32 iicSt,i;
    
    rSRCPND = BIT_IIC;
    rINTPND = BIT_IIC;
    iicSt   = rIICSTAT; 
    
    if(iicSt & 0x8){}
    if(iicSt & 0x4){}
    if(iicSt & 0x2){}
    if(iicSt & 0x1){}

    switch(_iicMode)
    {
       case POLLACK:
           _iicStatus = iicSt;
           break;

       case RDDATA:
           if((_iicDataCount--)==0)
           {
               _iicData[_iicPt++] = rIICDS;
            
               rIICSTAT = 0x90;
               rIICCON  = 0xaf;
               Delay(1);

               break;    
           }      
           _iicData[_iicPt++] = rIICDS;

           if((_iicDataCount)==0)
               rIICCON = 0x2f;
           else 
               rIICCON = 0xaf;
               break;

        case WRDATA:
            if((_iicDataCount--)==0)
            {
                rIICSTAT = 0xd0;
                rIICCON  = 0xaf;
                Delay(1);
                break;    
            }
            rIICDS = _iicData[_iicPt++];
            for(i=0;i<10;i++);
              
            rIICCON = 0xaf;
            break;

        case SETRDADDR:

            if((_iicDataCount--)==0)
                break; 
            rIICDS = _iicData[_iicPt++];
            for(i=0;i<10;i++);
            rIICCON = 0xaf;
            break;

        default:
            break;      
    }
}


void Test_Iic2(void)
{
    unsigned int i,j,save_E,save_PE;
    static U8 data[256];
    
    Uart_Printf("[ IIC Test(Polling) using 24C02 ]\n");

    save_E   = rGPECON;
    save_PE  = rGPEUP;

    rGPEUP  = 0xc000;
    rGPECON = 0xa0000000;

 
    rIICCON  = (1<<7) | (0<<6) | (1<<5) | (0xf);

    rIICADD  = 0x10;
    rIICSTAT = 0x10;
    
    Uart_Printf("Write test data into 24C02\n");

    for(i=0;i<256;i++)
        _Wr24C080(0xa0,(U8)i,255-i);
    for(i=0;i<256;i++)
        data[i] = 0;

    Uart_Printf("Read test data from 24C02\n");
    for(i=0;i<256;i++)
        _Rd24C080(0xa0,(U8)i,&(data[i])); 

    for(i=0;i<16;i++)
    {
        for(j=0;j<16;j++)
            Uart_Printf("%2x ",data[i*16+j]);
        Uart_Printf("\n");
    }
    
    rGPEUP  = save_PE;
    rGPECON = save_E;
}


void _Wr24C080(U32 slvAddr,U32 addr,U8 data)
{
    _iicMode      = WRDATA;
    _iicPt        = 0;
    _iicData[0]   = (U8)addr;
    _iicData[1]   = data;
    _iicDataCount = 2;
    
    rIICDS        = slvAddr;
    rIICSTAT      = 0xf0;      

    while(_iicDataCount!=-1)
       Run_IicPoll();

    _iicMode = POLLACK;

    while(1)
    {
        rIICDS     = slvAddr;
        _iicStatus = 0x100;
        rIICSTAT   = 0xf0;
        rIICCON    = 0xaf;
        while(_iicStatus==0x100)  
            Run_IicPoll();
              
        if(!(_iicStatus & 0x1))
            break;
    }
    rIICSTAT = 0xd0;
    rIICCON  = 0xaf;
    Delay(1);
}
        

void _Rd24C080(U32 slvAddr,U32 addr,U8 *data)
{
    _iicMode      = SETRDADDR;
    _iicPt        = 0;
    _iicData[0]   = (U8)addr;
    _iicDataCount = 1;

    rIICDS   = slvAddr;
    rIICSTAT = 0xf0;
    while(_iicDataCount!=-1)
        Run_IicPoll();

    _iicMode      = RDDATA;
    _iicPt        = 0;
    _iicDataCount = 1;
    
    rIICDS   = slvAddr;
    rIICSTAT = 0xb0;
    rIICCON  = 0xaf;  
    while(_iicDataCount!=-1)
        Run_IicPoll();

    *data = _iicData[1];
}


void Run_IicPoll(void)
{
    if(rIICCON & 0x10) 
       IicPoll();
}       
    

void IicPoll(void)
{
    U32 iicSt,i;
    
    iicSt = rIICSTAT; 
    if(iicSt & 0x8){}
    if(iicSt & 0x4){}
    if(iicSt & 0x2){}
    if(iicSt & 0x1){}

    switch(_iicMode)
    {
        case POLLACK:
            _iicStatus = iicSt;
            break;

        case RDDATA:
            if((_iicDataCount--)==0)
            {
                _iicData[_iicPt++] = rIICDS;
            
                rIICSTAT = 0x90;
                rIICCON  = 0xaf;
                Delay(1);
                break;    
            }      
            _iicData[_iicPt++] = rIICDS;
 
            if((_iicDataCount)==0)
                rIICCON = 0x2f;
            else 
                rIICCON = 0xaf;
            break;

        case WRDATA:
            if((_iicDataCount--)==0)
            {
                rIICSTAT = 0xd0;
                rIICCON  = 0xaf;
                Delay(1);

                break;    
            }
            rIICDS = _iicData[_iicPt++];
            for(i=0;i<10;i++);
            rIICCON = 0xaf;
            break;

        case SETRDADDR:
            if((_iicDataCount--)==0)
            {
                break;   
            }
            rIICDS = _iicData[_iicPt++];
            for(i=0;i<10;i++);
            rIICCON = 0xaf;
            break;

        default:
            break;      
    }
}
